function initDraggableAddControl(t){var e=JSON.parse(t.data);if(e){BX.loadScript("/bitrix/js/main/core/core_dragdrop.js",function(){(function a(){if(!!BX.DragDrop)window["dnd_parameter_"+t.propertyID]=new DragNDropAddParameterControl(e,t);else setTimeout(a,50)})()})}}function DragNDropAddParameterControl(t,e){var a=BX.util.getRandomString(5);this.params=e||{};this.useBigData=this.params.propertyParams.BIG_DATA&&this.params.propertyParams.BIG_DATA==="Y";this.message=JSON.parse(e.propertyParams.JS_MESSAGES)||{};this.nodes={countParamInput:this.getCountParamInput()};this.activeDragNode=false;this.temporarySortNode=false;this.itemRemoved=false;this.ids={to:"to_dnd_params_container_"+this.params.propertyID+"_"+a,from:"from_dnd_params_container_"+this.params.propertyID+"_"+a,label:"label_"+this.params.propertyID+"_"+a};this.baseItems=this.getBaseItems(t);this.sortedItems=this.getSortedItems(t);this.variantCounts=this.getVariantsCountMap(t);this.dragItemClassName="dnd-add-draggable-item-"+this.params.propertyID+"-"+a;this.lastEntered=null;this.timeOut=null;BX.loadCSS(this.getPath()+"/style.css?"+a);this.buildNodes();this.initDragDrop();this.saveData()}DragNDropAddParameterControl.prototype={getPath:function(){var t=this.params.propertyParams.JS_FILE.split("/");t.pop();return t.join("/")},getBaseItems:function(t){if(!t)return[];var e=[],a;for(a in t){if(t.hasOwnProperty(a)){e.push({variant:t[a].VARIANT,bigData:false,message:t[a].CODE})}}return e},getSortedItems:function(t){if(!t)return[];var e=this.params.oInput.value||"",a=[],r,s;try{s=JSON.parse(e.replace(/'/g,'"'))}catch(t){s=[]}for(r in s){if(s.hasOwnProperty(r)){for(var o in t){if(t.hasOwnProperty(o)&&t[o].VARIANT==s[r].VARIANT&&(!this.useBigData&&!s[r].BIG_DATA||this.useBigData)){a.push({variant:s[r].VARIANT,bigData:s[r].BIG_DATA,message:t[o].CODE})}}}}return a},buildNodes:function(){var t=BX.findParent(this.params.oCont,{className:"bxcompprop-prop-tr"}),e=BX.findChildren(t,{tagName:"td"}),a=BX.create("TR",{props:{className:"bxcompprop-prop-tr"}});if(e.length){e[0].setAttribute("colspan",2);e[0].setAttribute("style","text-align: center !important");e[1].setAttribute("colspan",2);a.appendChild(e[0]);t.parentNode.insertBefore(a,t)}this.nodes.rootTo=this.getToNode();this.nodes.rootFrom=this.getFromNode();this.nodes.summaryInfo=BX.create("DIV",{props:{className:"catalog-preset-summary"}});this.nodes.bigDataControl=this.useBigData?BX.create("DIV",{props:{className:"catalog-preset-bigdata-control"},children:[BX.create("LABEL",{attrs:{for:this.ids.label},children:[BX.create("INPUT",{props:{id:this.ids.label,type:"checkbox"},events:{change:BX.proxy(this.toggleBigData,this)}}),BX.create("SPAN",{text:" BigData"})]})]}):null;this.nodes.summary=BX.create("TABLE",{attrs:{width:"100%"},children:[BX.create("TR",{children:[BX.create("TD",{style:{verticalAlign:"bottom"},children:[this.nodes.summaryInfo]}),BX.create("TD",{style:{verticalAlign:"bottom"},children:[this.nodes.bigDataControl]})]})]});this.params.oCont.appendChild(BX.create("DIV",{props:{className:"dnd-add-common-container"},children:[this.nodes.summary,this.nodes.rootTo,this.nodes.rootFrom,BX.create("DIV",{props:{className:"catalog-preset-clear"}})]}))},getToNode:function(){var t=BX.create("DIV",{props:{id:this.ids.to,className:"catalog-preset-left"}});for(var e in this.sortedItems){if(this.sortedItems.hasOwnProperty(e)){t.appendChild(BX.create("DIV",{attrs:{"data-value":this.sortedItems[e].variant.toString(),"data-bigdata":this.sortedItems[e].bigData?"true":"false"},props:{type:"button",className:this.dragItemClassName+" dnd-add-draggable-control catalog-preset-shem catalog-preset-shem-"+this.sortedItems[e].message,title:this.message.variant+" "+this.sortedItems[e].message},children:[BX.create("DIV",{props:{className:"catalog-preset-shem-bigdata"}}),BX.create("DIV",{props:{className:"dnd-add-draggable-control-remove",title:this.message.delete},events:{click:BX.proxy(this.removeItem,this)}})],events:{dragstart:BX.delegate(function(){this.itemFromSortedList=BX.proxy_context},this),dragend:BX.delegate(function(){this.itemFromSortedList=false;this.disableActiveDropZone()},this)}}))}}return t},getFromNode:function(){var t=BX.create("DIV",{props:{id:this.ids.from,className:"catalog-preset-right"},children:[BX.create("DIV",{props:{className:"catalog-preset-center-arrow"},children:[BX.create("DIV",{props:{className:"catalog-preset-center-arrow-btn"},events:{click:BX.proxy(this.arrowClick,this)}})]})]});for(var e in this.baseItems){if(this.baseItems.hasOwnProperty(e)){t.appendChild(BX.create("DIV",{attrs:{"data-value":this.baseItems[e].variant.toString(),"data-bigdata":"false",draggable:"true"},props:{type:"button",className:"catalog-preset-shem catalog-preset-shem-"+this.baseItems[e].message+(e==0?" catalog-preset-selected":""),title:this.message.variant+" "+this.baseItems[e].message},children:[BX.create("DIV",{props:{className:"catalog-preset-shem-bigdata"}})],events:{click:BX.proxy(this.selectItem,this),dragstart:BX.proxy(function(t){t.dataTransfer.setData("text","");this.activeDragNode=BX.proxy_context.cloneNode(true);this.temporarySortNode=false;this.selectItem(t);BX.addClass(this.activeDragNode,"dnd-add-dragged-item")},this),drag:BX.proxy(function(t){BX.PreventDefault(t);this.dragdrop._ondrag(t);if(!BX.browser.IsFirefox()){if(this.temporarySortNode&&!this.dragdrop.sortableInterval){this.dragdrop.ondragStart(t,this.temporarySortNode)}if(!this.temporarySortNode&&this.dragdrop.sortableInterval){this.dragdrop.ondragEnd(t);this.dragdrop.sortableInterval=false}}},this),dragend:BX.proxy(function(t){BX.PreventDefault(t);BX.removeClass(this.temporarySortNode,"draggable-active");this.disableActiveDropZone();if(this.dragdrop.sortableInterval){this.dragdrop.ondragEnd(t,this.temporarySortNode);this.dragdrop.sortableInterval=false}this.activeDragNode=false;this.temporarySortNode=false},this)}}))}}return t},selectItem:function(t){var e=BX.getEventTarget(t),a=this.nodes.rootFrom.querySelectorAll(".catalog-preset-shem"),r,s;if(e&&!BX.hasClass(e,"catalog-preset-shem")){e=BX.findParent(e,{className:"catalog-preset-shem"},this.nodes.rootFrom)}if(!e)return;s=e.getAttribute("data-value");for(r in a){if(a.hasOwnProperty(r)){if(a[r].getAttribute("data-value")===s){BX.addClass(a[r],"catalog-preset-selected")}else{BX.removeClass(a[r],"catalog-preset-selected")}}}},removeItem:function(t){var e=BX.getEventTarget(t),a;if(!e)return;a=BX.findParent(e,{className:"dnd-add-draggable-control"});if(a){this.nodes.rootTo.removeChild(a);this.dragdrop.removeSortableItem(a)}this.saveData();BX.PreventDefault(t)},initDragDrop:function(){if(BX.isNodeInDom(this.params.oCont)){this.dragdrop=BX.DragDrop.create({dragItemClassName:this.dragItemClassName,dragItemControlClassName:"dnd-add-draggable-control",sortable:{rootElem:this.nodes.rootTo},dragEnd:BX.delegate(function(){this.saveData()},this)});BX.bind(this.nodes.rootTo,"dragenter",BX.delegate(this.onDragEnter,this));BX.bind(this.nodes.rootTo,"dragover",BX.delegate(this.onDragOver,this));BX.bind(this.nodes.rootTo,"dragleave",BX.delegate(this.onDragLeave,this))}else{setTimeout(BX.delegate(this.initDragDrop,this),50)}},toggleBigData:function(t){var e=BX.getEventTarget(t),a,r;if(!e)return;a=this.nodes.rootFrom.querySelectorAll("[data-bigdata]");r=a.length;while(r--){a[r].setAttribute("data-bigdata",!!e.checked?"true":"false")}},onDragEnter:function(t){BX.eventReturnFalse(t);this.lastEntered=t.target},onDragOver:function(t){BX.eventReturnFalse(t);this.enableActiveDropZone();if(this.activeDragNode&&!this.temporarySortNode){this.temporarySortNode=this.getTemporaryNodeClone(this.activeDragNode);BX.addClass(this.temporarySortNode,"draggable-active");this.nodes.rootTo.appendChild(this.temporarySortNode);this.dragdrop.addDragItem([this.temporarySortNode]);this.dragdrop.addSortableItem(this.temporarySortNode);this.saveData()}if(this.itemFromSortedList&&this.itemRemoved){this.nodes.rootTo.appendChild(this.itemFromSortedList);this.dragdrop.addDragItem([this.itemFromSortedList]);this.dragdrop.addSortableItem(this.itemFromSortedList);this.temporarySortNode=false;this.itemRemoved=false;this.saveData()}},onDragLeave:function(t){BX.eventReturnFalse(t);if(this.lastEntered!==t.target){return}var e=document.elementFromPoint(t.pageX,t.pageY);if(!e||!this.nodes.rootTo.contains(e)){this.disableActiveDropZone();if(this.temporarySortNode){this.nodes.rootTo.removeChild(this.temporarySortNode);this.dragdrop.removeSortableItem(this.temporarySortNode);this.dragdrop.isSortableActive=false;this.temporarySortNode=false;this.saveData()}if(this.itemFromSortedList&&!this.itemRemoved){this.nodes.rootTo.removeChild(this.itemFromSortedList);this.dragdrop.removeSortableItem(this.itemFromSortedList);this.dragdrop.isSortableActive=false;this.temporarySortNode=false;this.itemRemoved=true;this.saveData()}}},getTemporaryNodeClone:function(t){var e=t.cloneNode(true);BX.removeClass(e,"dnd-add-dragged-item catalog-preset-selected");BX.addClass(e,"dnd-add-draggable-control "+this.dragItemClassName);BX.unbindAll(e);BX.bind(e,"dragstart",BX.delegate(function(){this.itemFromSortedList=BX.proxy_context},this));BX.bind(e,"dragend",BX.delegate(function(){this.itemFromSortedList=false},this));e.appendChild(BX.create("DIV",{props:{className:"dnd-add-draggable-control-remove",title:this.message.delete},events:{click:BX.delegate(this.removeItem,this)}}));return e},enableActiveDropZone:function(){BX.addClass(this.nodes.rootTo,"drop-zone-active")},disableActiveDropZone:function(){BX.removeClass(this.nodes.rootTo,"drop-zone-active")},saveData:function(){var t=this.nodes.rootTo.querySelectorAll("."+this.dragItemClassName),e=[];for(var a in t){if(t.hasOwnProperty(a)){e.push({VARIANT:t[a].getAttribute("data-value"),BIG_DATA:t[a].getAttribute("data-bigdata")==="true"})}}this.params.oInput.value=JSON.stringify(e).replace(/"/g,"'");if(this.timeOut){this.timeOut=clearTimeout(this.timeOut)}this.timeOut=setTimeout(BX.proxy(function(){this.setElementCount(e)},this),20)},getCountParamInput:function(){var t=BX.findParent(this.params.oCont,{className:"bxcompprop-content"}),e=null,a=this.params.propertyParams.COUNT_PARAM_NAME||"";if(t&&a){e=t.querySelector('[data-bx-property-id="'+a+'"]')}return e},setElementCount:function(t){var e,a,r;e=this.getElementCount(t,false);a=this.getElementCount(t,true);if(this.nodes.countParamInput){this.nodes.countParamInput.value=e}r=this.message.quantity+" - "+e+"<br />";r+=a?this.message.quantityBigData+" - "+a:"";this.nodes.summaryInfo.innerHTML=r},getElementCount:function(t,e){var a=0;for(var r in t){if(t.hasOwnProperty(r)){if(e&&t[r].BIG_DATA||!e&&!t[r].BIG_DATA){a+=parseInt(this.variantCounts[t[r].VARIANT])}}}return a},getVariantsCountMap:function(t){var e={};for(var a in t){if(t.hasOwnProperty(a)){e[t[a].VARIANT]=t[a].COUNT}}return e},arrowClick:function(){var t=this.nodes.rootFrom.querySelector(".catalog-preset-selected")||this.nodes.rootFrom.querySelector(".catalog-preset-shem"),e;if(t){e=this.getTemporaryNodeClone(t);this.nodes.rootTo.appendChild(e);this.dragdrop.addDragItem([e]);this.dragdrop.addSortableItem(e);this.saveData()}}};